package com.ww.scala.syntax

import java.util

/**
 * 偏函数
 */
object syntax08_implicate {

  def main(args: Array[String]): Unit = {
    val ints = new util.ArrayList[Int]()
    ints.add(1)
    ints.add(2)
    ints.add(3)

    def foreach[T](list: util.List[T], f: (T) => Unit): Unit = {
      val iterator = list.iterator()
      while (iterator.hasNext) f(iterator.next())
    }

    foreach(ints, println)


    println("===================隐式转换1======================")
    val value = new Xxx[Int](ints)
    value.foreach(println)
    println("===================隐式转换2======================")

    implicit def x[T](list: util.List[T]): Xxx[T] = {
      new Xxx[T](list)
    }

    ints.foreach(println)
    println("===================隐式转换3======================")
    implicit def x1[T](list: util.ArrayList[T]): Ooo[T] = {
      val v = list.iterator()
      new Ooo[T](v)
    }

    implicit def x2[T](list: util.LinkedList[T]): Ooo[T] = {
      val v = list.iterator()
      new Ooo[T](v)
    }
    val ints2 = new util.LinkedList[Int]()
    ints2.add(1)
    ints2.add(2)
    ints2.add(3)
    ints2.foreach(println)
    ints.foreach(println)
    println("===================隐式转换4======================")

    implicit val x3:String = "lisi"

    def xx(implicit name:String): Unit ={
      println(name)
    }
    xx
    println("===================隐式转换5======================")
    def xx2(age:Int)(implicit name:String): Unit ={
      println(name+" "+ age)
    }
    xx2(10)


  }

  class Xxx[T](list: util.List[T]) {

    def foreach(f: (T) => Unit): Unit = {
      val iterator = list.iterator()
      while (iterator.hasNext) f(iterator.next())
    }
  }

  class Ooo[T](iter: util.Iterator[T]) {
    def foreach(f: (T) => Unit): Unit = {
      while (iter.hasNext) f(iter.next)
    }
  }


}


